package com.losers;

import java.io.IOException;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
//import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;

public class CameraActivity extends Activity implements SurfaceHolder.Callback {
	
	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;
	private Camera camera;
	private boolean isPreview;
	
	private Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
	public void onPictureTaken(byte[] imageData, Camera c) {
		Log.w("YYYY", "YYYY");
		}
		};;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Configure window
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Use camera.xml as content view
        setContentView(R.layout.camera);

        // Get surface view and initialize surface holder
        surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        
        
        
        /**/
    }

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		
		// Surface size is changed so stop preview
		if (isPreview) {
			camera.stopPreview();
		}

		// Set camera properties to have correct preview size
		Camera.Parameters p = camera.getParameters();
		p.setPreviewSize(width, height);
		camera.setParameters(p);

		try {
			camera.setPreviewDisplay(holder);
		} 
		catch (IOException e) {
			e.printStackTrace();
		}

		// Start preview again
		camera.startPreview();
		isPreview = true;
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// start camera
		camera = Camera.open();	
		
		//camera.takePicture(shutter, raw, jpeg)
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// stop preview and release camera
		camera.stopPreview();
		isPreview = false;
		camera.release();
	}

	/*@Override
	public void onPictureTaken(byte[] data, Camera camera) {
		
		Log.w("IIII","IIII");
	}*/
	
		
	PictureCallback pictureCallBack = new PictureCallback() {
		  public void onPictureTaken(byte[] _data, Camera _camera) {

			  Log.w("IIIIx","IIIIx");
			  
			  if (_data != null) {

				  PictureForm.bmp = BitmapFactory.decodeByteArray(_data, 0, _data.length);
				  //Database db = new Database(this);
				  //ContentValues cv = new ContentValues();
				  
				  //cv.put("picture", _data);
				  
				  //db.getWritableDatabase().execSQL("")
				  
					/*Intent mIntent = new Intent();

					FileUtilities.StoreByteImage(mContext, imageData,
							 50, "ImageName.jpg");
					mCamera.startPreview();
					
					setResult(FOTO_MODE,mIntent);*/
					finish();
				}
			  
			  Log.w("IIIIe","IIIIe");
			  
		  }
		};

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		
		// Camera-button is disabled on emulator so allow Call-button also to take picture
	    if ((keyCode == KeyEvent.KEYCODE_CALL || keyCode == KeyEvent.KEYCODE_CAMERA) && 
	    		event.isTracking() && !event.isCanceled()) {
	        
	    	
	    	camera.takePicture(null, null, pictureCallBack);
	    	
	        return true;
	    }
	    return super.onKeyUp(keyCode, event);
	}
}